package controller;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
import app.fitness.fragment.DashBoardFragment;
import app.fitness.fragment.PersonalDataFragment;
import app.fitness.fragment.UserProfileFragment;

import com.razer.android.nabuopensdk.interfaces.UserProfileListener;
import com.razer.android.nabuopensdk.models.UserProfile;
import com.razerzone.android.fitness.ActivityFitness;

import define.Constants;
import define.DashBoard;
import define.PersonalData;

public class UserProfileDataAsync extends AsyncTask<Void, Integer, Boolean> {

	/**
	 * String section
	 */
	// public static boolean IS_SIGNED_IN = false;

	private String NOTIFICATION = null;

	private final String TAG = UserProfileDataAsync.class.getSimpleName();

	/**
	 * The others section
	 */
	private Context context;

	public UserProfileDataAsync(Context context) {
		this.context = context;
	}

	@Override
	protected Boolean doInBackground(Void... params) {
		/**
		 * Get User Profile to show
		 */
		Constants.nabuSDK.getUserProfile(context, new UserProfileListener() {

			@Override
			public void onReceiveFailed(String error) {
				Log.i(TAG, "onReceiveFailed " + error);

				NOTIFICATION = error;
				System.exit(0);

				if (PersonalDataFragment.iTaskCallBacks != null) {
					PersonalDataFragment.iTaskCallBacks.onCompleteReceiveData();
				}
				// if (UserProfileFragment.iTaskCallBacks != null) {
				// UserProfileFragment.iTaskCallBacks.onCompleteReceiveData();
				// }
			}

			@Override
			public void onReceiveData(UserProfile userProfile) {
				/**
				 * Provide the other information
				 */
				// Store user profile data to load into User Profile page
				define.UserProfile.userProfileData
						.setBirthDate(userProfile.birthDay + " "
								+ userProfile.birthMonth + " "
								+ userProfile.birtyYear);
				define.UserProfile.userProfileData
						.setFirstName(userProfile.firstname);
				define.UserProfile.userProfileData
						.setGender(userProfile.gender);

				define.UserProfile.userProfileData
						.setHeight(userProfile.height);
				define.UserProfile.userProfileData
						.setLastName(userProfile.lastname);
				define.UserProfile.userProfileData
						.setNickName(userProfile.nickName);
				define.UserProfile.userProfileData
						.setRazerID(userProfile.razerid);
				define.UserProfile.userProfileData
						.setUrlAvatar(userProfile.avatarUrl);
				define.UserProfile.userProfileData
						.setWeight(userProfile.weight);

				/**
				 * Save Nick name into single ton to load later
				 */
				DashBoardFragment.singleTon.setNickName(userProfile.nickName);

				/**
				 * Check if Fragment is Dash Board Fragment
				 */
				if (DashBoard.mFmDashBoard instanceof DashBoardFragment) {
					/**
					 * Set User Name
					 */
					ActivityFitness.mTvNickNameBesideAvatar
							.setText(define.UserProfile.userProfileData
									.getNickName());
					ActivityFitness.mTvNickNameBesideDevice
							.setText(define.UserProfile.userProfileData
									.getNickName());

					/**
					 * Set avatar for user
					 */
					new DownloadImageAsync(ActivityFitness.mIbtnAvatarInDrawer)
							.execute(define.UserProfile.userProfileData
									.getUrlAvatar());
					// }
				}

				/**
				 * Check if Fragment is Personal Data Fragment
				 */
				if (PersonalData.mFmPersonalData instanceof PersonalDataFragment) {
					PersonalData.mFmPersonalData = null;

					/**
					 * Set data into the views also
					 */
					PersonalDataFragment.initialData(context);

					/**
					 * Set User Name
					 */
					ActivityFitness.mTvNickNameBesideAvatar
							.setText(define.UserProfile.userProfileData
									.getNickName());
					ActivityFitness.mTvNickNameBesideDevice
							.setText(define.UserProfile.userProfileData
									.getNickName());

					/**
					 * Set avatar for user
					 */
					// Load image
					if (DownloadImageAsync.getBitmap() != null) {
						ActivityFitness.mIbtnAvatarInDrawer
								.setImageBitmap(DownloadImageAsync.getBitmap());
					} else {
						new DownloadImageAsync(
								ActivityFitness.mIbtnAvatarInDrawer)
								.execute(define.UserProfile.userProfileData
										.getUrlAvatar());
					}

					if (PersonalDataFragment.iTaskCallBacks != null) {
						PersonalDataFragment.iTaskCallBacks
								.onCompleteReceiveData();
					}
				}

				/**
				 * Check if Fragment is User Profile Fragment
				 */
				if (define.UserProfile.mFmUserProfile instanceof UserProfileFragment) {
					define.UserProfile.mFmUserProfile = null;

					/**
					 * Set data into the views also
					 */
					// UserProfileFragment.initialData(context);
					//
					// if (UserProfileFragment.iTaskCallBacks != null) {
					// UserProfileFragment.iTaskCallBacks
					// .onCompleteReceiveData();
					// }
				}

				// IS_SIGNED_IN = true;
			}
		});
		return true;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();

		if (PersonalDataFragment.iTaskCallBacks != null) {
			PersonalDataFragment.iTaskCallBacks.onBeginReceiveData();
		}
	}

	@Override
	protected void onPostExecute(Boolean result) {
		super.onPostExecute(result);

		if (NOTIFICATION != null && !NOTIFICATION.equals("")) {
			// If there is error
			Toast.makeText(
					context,
					"Get User Profile failed! \n"
							+ "Clear application cache via Application Manager in Settings \n"
							+ "And try again", Toast.LENGTH_SHORT).show();
		}
	}
}
